package org.example.myleet.p1380;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
    public List<Integer> luckyNumbers (int[][] matrix) {
        List<Integer> result = new ArrayList<>();
        int m = matrix.length, n = matrix[0].length;
        int[] rMin = new int[m], cMax = new int[n];
        Arrays.fill(rMin, Integer.MAX_VALUE);
        Arrays.fill(cMax, Integer.MIN_VALUE);
        for (int r = 0; r < m; ++r) {
            for (int c = 0; c < n; ++c) {
                rMin[r] = Math.min(rMin[r], matrix[r][c]);
                cMax[c] = Math.max(cMax[c], matrix[r][c]);
            }
        }
        for (int r = 0; r < m; ++r) {
            for (int c = 0; c < n; ++c) {
                if (matrix[r][c] == rMin[r] && matrix[r][c] == cMax[c]) {
                    result.add(matrix[r][c]);
                }
            }
        }
        return result;
    }
}
